<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <link rel="stylesheet" type="text/css" href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
        <link rel="stylesheet" type="text/css"
            href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
        <script type="text/javascript" src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>   
        <script type="text/javascript"
            src="<c:url value="/static/js/common.js"/>"></script>       
        <script>
        var map = {};//存入 材料公斤数 map
        var selectMap = {};//材料是否被选择map
        var checkflag = true;//判断是否第一次进界面  设置map 值的标志
	function initList(listDisplayEId) {
		$('#' + listDisplayEId).datagrid({
			nowrap : false,
			striped : true,
			collapsible : true,
			remoteSort : true,
			notSelectByRow : false,
			onUncheck:function(rowIndex,rowData) { 
				delete selectMap[rowData['materId']];
			},
	        onCheck:function(rowIndex,rowData) {
	        	selectMap[rowData['materId']]=1;
	        },
			onLoadError : function() {
				$.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
			},
			onLoadSuccess:function(){ 
				//初始化 已选项目 设置重量
				var checkBoxs =$("input[name='checkBox']:eq(0)").val();
                var checkBoxArry = checkBoxs.split("|");
				if(checkflag){
		            for(var i=0 ; i<checkBoxArry.length;i++){
		            	var checkAndWeightArry = checkBoxArry[i].split("-");
                        map[checkAndWeightArry[0]] = checkAndWeightArry[1];
                        selectMap[checkAndWeightArry[0]] = 1;
		            }
					checkflag = false;
				}
				//初始化 复选框
                var allrows = $('#listDisplay').datagrid("getRows");
                for (var i = 0; i < allrows.length; i++) {
                    var rowIndex = $('#listDisplay').datagrid('getRowIndex', allrows[i]['materId']);
                    var hasSelect = allrows[i]['materId'] in selectMap;
                    if(hasSelect){
                    	$('#listDisplay').datagrid("checkRow",rowIndex);
                    }
                }
				
                var rows = $('#'+listDisplayEId).datagrid('getRows');   
                if(rows.length > 0){

                    for (var i = 0 ; i < rows.length;i++){

                        var key = rows[i]['materId'];
                        var has = key in map; 
                        if(has){
                            rows[i]['materWeight'] = map[key];
                            $('#listDisplay').datagrid('refreshRow', i);
                        }
                    }
                }
                $(".datagrid-row").mouseover(function(e){  
                    if(checkHover(e,this)){
                        var foodId = $(this).find('td').eq(1).text();
                        var index = $('#listDisplay').datagrid('getRowIndex', foodId);
                        $('#listDisplay').datagrid('beginEdit',index);
                    }
                });   
                $(".datagrid-row").mouseout(function(e){  
                    if(checkHover(e,this)){
                        var foodId2 = $(this).find('td').eq(1).text();
                        var index2 = $('#listDisplay').datagrid('getRowIndex', foodId2);
                        $('#listDisplay').datagrid('endEdit',index2);
                        if( $(this).find('td').eq(5).text()!=''){
                            map[$(this).find('td').eq(1).text()] = $(this).find('td').eq(5).text(); 
                        }else{
                            delete map[$(this).find('td').eq(1).text()];
                        }
                        
                    }
                });   
            } 
		});
	};
	$(document).ready(function() {
		initList('listDisplay');
	});

	function searchInfo(){
        if($('#materType').combobox('getValue') ==''){
            $('#listDisplay').datagrid('load', {
                materType : 0,
                materName : $('#materName').val()
            }); 
        }else{
            $('#listDisplay').datagrid('load', {
                materType : $('#materType').combobox('getValue'),
                materName : $('#materName').val()
            });  
        }
     }
	//添加信息按钮响应函数
	function addFood() {
		$('#AddFoodForm')
				.form('submit',{
							url : '<c:url value='/food/updateFood.do'/>', //查询提交路径
							onSubmit : function()//提交按钮相应函数
							{
								var listDisplayEId = "listDisplay";
								var row = $('#' + listDisplayEId).datagrid('getSelections');
								if (row.length == 0) {
									$.messager.alert('提示', '请选择一条记录操作！', 'info');
									return;
								} else {
									//遍历map 组成json串串
					                var json ="";
					                for(aa in map){
					                	var hasSelect = selectMap[aa];//判断是否选上
					                	if(hasSelect){
		                                     json=json + ',{materId:"'+aa +'",materWeight:"'+ map[aa]+'"}';
					                	}
					                }
					                json = "["+json.substring(1)+"]";
									document.getElementsByName("fooddategridList")[0].value = json;
								}
								return $('#AddFoodForm').form('validate');
							},
							success : function(data) {
								data = eval('(' + data + ')');
								if (data.success) {
									$.messager.alert('系统提示', '修改成功', 'info', function(){
										window.opener.window.tea();
			                            window.close();
			                        });
								} else {
									$.messager.alert("提示", data.msg, 'warning');
								}
							},
							error : function() {
								$.messager.alert("错误", "系统错误，请联系管理员！", 'error');
								return;
							}
						});
	}
	$(function() {        
	    // 下拉框选择控件，下拉框的内容是动态查询数据库信息  
	    	$('#foodType').combobox({   
	            url:'<c:url value='/food/pulldownFoodType.do'/>',   
	            editable:false, //不可编辑状态  
	            cache: false,  
	            panelHeight: '150',  
	            valueField:'foodType',     
	            textField:'foodTypename',  
	            required:true
	    	});      
	      
	    // 下拉框选择控件，下拉框的内容是动态查询数据库信息  
	    	$('#foodType1').combobox({   
	            url:'<c:url value='/food/pulldownFoodType1.do'/>',   
	            editable:false, //不可编辑状态  
	            cache: false,  
	            panelHeight: '150',  
	            valueField:'foodType1',     
	            textField:'foodTypename1',  
	            required:true
	            
	    	});      
	    	$('#materType').combobox({   
                data:[{"materType":"0","ordStateName":"全部"},{"materType":"1","ordStateName":"牛肉类"},{"materType":"2","ordStateName":"海鲜类 "},{"materType":"3","ordStateName":"水发类"},{"materType":"4","ordStateName":"猪肉类"},{"materType":"5","ordStateName":"浇头"},{"materType":"6","ordStateName":"鸭肉类"},{"materType":"7","ordStateName":"鸡肉类"},{"materType":"8","ordStateName":"鸡蛋类"},{"materType":"9","ordStateName":"鹌鹑类"},{"materType":"10","ordStateName":"河鲜类"},{"materType":"11","ordStateName":"豆制品类"},{"materType":"12","ordStateName":"根茎类"},{"materType":"13","ordStateName":"鸭蛋类"},{"materType":"14","ordStateName":"干货类"},{"materType":"15","ordStateName":"绿叶菜"},{"materType":"16","ordStateName":"瓜果类"},{"materType":"17","ordStateName":"叶菜"},{"materType":"18","ordStateName":"其他类"},{"materType":"19","ordStateName":"猪骨类"},{"materType":"20","ordStateName":"蛋类"},{"materType":"21","ordStateName":"酸辣"},{"materType":"22","ordStateName":"咸菜类"},{"materType":"23","ordStateName":"鱼头类"},{"materType":"24","ordStateName":"猪血"}],   
                editable:false, //不可编辑状态  
                cache: false,  
                valueField:'materType',     
                textField:'ordStateName',  
            });
	}); 
	function formateCheckbox(value, row, index) {
		return '<input type="checkbox" name="DataGridCheckbox" />';
	}

</script>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<table width="90%" align="center">
		<tr></tr>
		<tr>
			<td>

				<div style="height: auto; background: #fafafa; padding: 5px;"
					class="easyui-panel" title="添加用户信息" iconCls="icon-save"
					collapsible="true" minimizable="false" maximizable="false"
					closable="false">
						<table cellpadding="5">
						<form id="AddFoodForm" name="AddFoodForm" method="post">
						
							<tr>
								<td>菜品名:</td>
								<td><input class="easyui-validatebox" type="text"
									name="foodName" required="true" validType="length[1,30]"
									value="${foodVo.foodName }" readOnly="true"/></td>
							</tr>
							<tr>
								<td>品别:</td>
								<td>
									<input class="easyui-combobox"   name="foodType" id="foodType" required="true" value="${foodVo.foodType }">
							    </td>
							</tr>
							<tr>
								<td>类别:</td>
								<td>
									<input class="easyui-combobox"   name="foodType1" id="foodType1" required="true" value="${foodVo.foodType1 }">
								</td>
							</tr>
							<tr>
								<td>主料大全:</td>
								<td><input class="easyui-validatebox" type="text"
									name="foodMtype" required="true" validType="length[1,30]"
									value="${foodVo.foodMtype }" /></td>
							</tr>
							<tr>
								<td>主料半成品名称:</td>
								<td><input class="easyui-validatebox" type="text"
									name="foodMname" required="true" validType="length[1,30]"
									value="${foodVo.foodMname }" /></td>
							</tr>
							<tr>
								<td><input class="easyui-validatebox" type="hidden"
									name="foodId" value="${foodVo.foodId }" /></td>
							</tr>
							<tr>
								<td><input class="easyui-validatebox" type="hidden"
									name="fooddategridList" /></td>
							</tr>
							<tr>
								<td><input class="easyui-validatebox" type="hidden" value="${foodVo.checkBox }"
									name="checkBox" /></td>
							</tr>
                        </form>
						</table>
                    
                    <table cellpadding="5">
                    <tr>
                       <td colspan="2">
                           <div style="height: auto; background: #fafafa; padding: 5px;" class="easyui-panel" title="搜索" iconCls="icon-save" 
                                    collapsible="true" minimizable="false" maximizable="false" closable="false">
                               <form id="searchForm" name="searchForm" method="post"
                                    action="userlist.txt"
                                    onsubmit="return searchDGData(this.id,'listDisplay');">
                                   <div>
                                       <table cellpadding="5">
                                           <tr>
                                               <td>材料名：</td>
                                               <td><input class="easyui-validatebox" id="materName" editable="true" name="materName"></input></td>
                                               <td>类别：</td>
                                               <td><input class="easyui-combobox" id="materType" editable="false" name="materType"></input></td>
                                               <td><input type="button" onclick="searchInfo()" value="查询"></td>
                                           </tr>
                                       </table>
                                   </div>
                               </form>
                          </div>
                       </td>
                   </tr>
                   </table>
                    
						<table width="98%">
							<tr>

							</tr>
							<tr style="height: 12px;"></tr>
							<tr>
								<td>
									<table id="listDisplay" title="材料列表" iconCls="icon-list"
										singleSelect="false" idField="materId"
										url="<c:url value="/mater/findEditorMater.do"/>" loadMsg="请稍候......"
										pagination="true" pageSize="10" pageList="[10,20,20,20,50]"
										sortName="materId" rownumbers="true" sortOrder="asc">
										<thead>
											<tr>
												<th field="materRole" width="25" align="center" formatter="formateCheckbox"
													checkbox="true"></th>
												<th field="materId" width="80" formatter="formatDGField"
													sortable="true">材料ID</th>
												<th field="materName" width="80" formatter="formatDGField"
													sortable="true">材料名</th>
												<th field="materType" width="60" formatter="formatDGField"
													sortable="true">种类</th>
												<th field="materDescribe" width="150"
													formatter="formatDGField" sortable="true">描述</th>
												<th field="materWeight" width="150"
													formatter="formatDGField" sortable="true" editor="text">
													重量(克)</th>
											</tr>
										</thead>
									</table>
								</td>
							</tr>
						</table>
						<a class="easyui-linkbutton" onclick="addFood()">修改</a>
					

				</div>
			</td>
		</tr>
	</table>
</body>
</html>